package com.sh.caryr.erp.custmz;


import com.sh.caryr.erp.entity.user.User;
import com.sh.caryr.erp.entity.user.UserRoleAuth;
import com.sh.caryr.erp.service.IUserService;
import com.sh.caryr.erp.util.ERPConstants;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class UserAuthorityContainer {

    @Autowired
    private IUserService userService;

    private User user;
    private List<UserRoleAuth> data = new ArrayList<>();

    public UserAuthorityContainer() {
    }

    public UserAuthorityContainer(List<UserRoleAuth> authList) {
        data = authList;
    }

    public UserAuthorityContainer(User user) {
        this.user = user;
        if (user != null && user.getUserRole() != null && user.getUserRole().getAuthList() != null) {
            data = user.getUserRole().getAuthList();
        }
    }

    public static UserAuthorityContainer allAuthsData() {
        List<UserRoleAuth> data = new ArrayList<>();
        data.add(new UserRoleAuth(ERPConstants.AUTH_TARGET_ALL, "", "", ""));
        UserAuthorityContainer authContainer = new UserAuthorityContainer(data);
        return authContainer;
    }

    public boolean check(String target, String actvt, String field, String value) {
        boolean result = false;
        if (user.getIsSuperuser() == ERPConstants.ENITYT_ACTIVE) {
            return true;
        }
        for (UserRoleAuth auth : data) {
            if (auth.getAuthTarget().equals(ERPConstants.AUTH_TARGET_ALL)) {
                return true;
            }
        }
        if (value == null) {
            result = check(target, actvt, field);
        } else {
            for (UserRoleAuth auth : data) {
                if (target.equals(auth.getAuthTarget()) && actvt.equals(auth.getAuthActvt())) {
                    if (ERPConstants.AUTH_VALUE_ALL.equals(auth.getAuthField())
                            || (field.equals(auth.getAuthField()) && value.equals(auth.getAuthValue()))) {
                        result = true;
                        break;
                    }
                }
            }
        }
        return result;
    }

    static final String seperator = "@@";
    public static boolean check(User user,String checkAuth){
        boolean result = false;
        String userAuthString = convertUserAuthToString(user);
        return userAuthString.contains(checkAuth+seperator);
    }

    public static String convertUserAuthToString(User user){
        StringBuffer buffer = new StringBuffer();
        if (user.getUserRole() != null && user.getUserRole().getId() != null && user.getUserRole().getId() != 0) {
            List<UserRoleAuth> authList = user.getUserRole().getAuthList();
            for(UserRoleAuth auth : authList){
                String authStr = "";
                if(auth.authTarget!=null&&!auth.authTarget.equals("")){
                    authStr += auth.authTarget;
                }
                if(auth.authActvt!=null&&!auth.authActvt.equals("")){
                    authStr += "_"+auth.authActvt;
                }
                if(auth.authField!=null&&!auth.authField.equals("")){
                    authStr += "_"+auth.authField;
                }
                if(auth.authValue!=null&&!auth.authValue.equals("")){
                    authStr += "_"+auth.authValue;
                }
                buffer.append(authStr+seperator);
            }
        }
        return buffer.toString();
    }
    private boolean check(String target, String actvt, String field) {
        boolean result = false;
        if (field == null || ERPConstants.AUTH_VALUE_ANY.equals(field)) {
            result = check(target, actvt);
        } else {
            for (int i = 0; i < data.size(); i++) {
                UserRoleAuth auth = data.get(i);
                if (target.equals(auth.getAuthTarget()) &&
                        actvt.equals(auth.getAuthActvt()) &&
                        field.equals(auth.getAuthField())) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    private boolean check(String target, String actvt) {
        boolean result = false;
        if (actvt == null) {
            result = check(target);
        } else {
            for (int i = 0; i < data.size(); i++) {
                UserRoleAuth auth = data.get(i);
                if (target.equals(auth.getAuthTarget()) &&
                        actvt.equals(auth.getAuthActvt())) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    private boolean check(String target) {
        boolean result = false;
        if (target != null) {
            for (int i = 0; i < data.size(); i++) {
                UserRoleAuth auth = data.get(i);
                if (target.equals(auth.getAuthTarget())) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    public List<UserRoleAuth> getData() {
        return data;
    }

    public void setData(List<UserRoleAuth> data) {
        this.data = data;
    }
}